import java.awt.*;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.ServerMessageEvent;

public class AutoFletcher extends Script implements PaintListener, ServerMessageListener {
	
	static int knife = 946;
	
	static int[] bankbooth = { 11402, 26972, 2213, 35647, 36786, 25808, 2213, 34752, 11758 };
	
	int state = 0;
	int logid = 0;
	int bowid = 0;
	int inter = 304;
	int x, y;
		
	public int cut = 0;
	public long startTime = System.currentTimeMillis();
    public int startLevel = skills.getCurrentSkillLevel(9);
    public int startXP = skills.getCurrentSkillExp(9);
	
	public String log = "";
	public String bow = "";
	
	public double getVersion() {
		return 0.1;
	}

	public String getScriptCategory() {
		return "Fletching";
	}

	public String getName() {
		return "AutoFletcher";
	}

	public String getAuthor() {
		return "Zachafer";
	}
	
	public void serverMessageRecieved(ServerMessageEvent e) {
		if (e.getMessage().indexOf("ou carefully cut the") != -1) cut++;
    }

	public void onRepaint(Graphics g) {
        if (isLoggedIn()) {
            long millis = System.currentTimeMillis() - startTime;
            long hours = millis / (1000 * 60 * 60);
            millis -= hours * 1000 * 60 * 60;
            long minutes = millis / (1000 * 60);
            millis -= minutes * 1000 * 60;
            long seconds = millis / 1000;
            int XPChange = skills.getCurrentSkillExp(9) - startXP;
            int LevelChange = skills.getCurrentSkillLevel(9) - startLevel;
            g.setColor(Color.white);
			g.drawString("Cut " + cut + " " + log + " " + bow + "s.", 18, 265);
			g.drawString("Level " + skills.getCurrentSkillLevel(9)
                    + " Fletching, " + skills.getXPToNextLevel(9)
                    + " exp until next level. (" + skills.getPercentToNextLevel(9) + "%)", 18, 280);
			g.drawString("Gained " + LevelChange + " levels and " + XPChange
                    + " exp.", 18, 295);
			g.drawString("Runtime: " + hours + " hours " + minutes
                    + " minutes " + seconds + " seconds.", 18, 310);
        }
    }
	
	public boolean onStart(Map<String, String> args) {
		log = args.get("logs");
		bow = args.get("type");
		if (log.equals("Normal")) {
			logid = 1511;
			inter = 305;
			if (bow.equals("Shortbow")) {
				bowid = 50;
				x = 200;
				y = 400;
			} else if(bow.equals("Longbow")) {
				x = 320;
				y = 403;
				bowid = 48;
			} else { //then it must be shafts
				x = 80;
				y = 400;
				bowid = 52;
			}
		} else if (log.equals("Oak")) {
			logid = 1521;
			if (bow.equals("Shortbow")) {
				bowid = 54;
			} else {
				bowid = 56;
			}
		} else if (log.equals("Willow")) {
			logid = 1519;
			if (bow.equals("Shortbow")) {
				bowid = 60;
			} else {
				bowid = 58;
			}
		} else if(log.equals("Maple")) {
			logid = 1517;
			if (bow.equals("Shortbow")) {
				bowid = 64;
			} else {
				bowid = 62;
			}
		} else if(log.equals("Yew")) {
			logid = 1515;
			if (bow.equals("Shortbow")) {
				bowid = 68;
			} else {
				bowid = 66;
			}
		} else if(log.equals("Magic")) {
			logid = 1513;
			if (bow.equals("Shortbow")) {
				bowid = 72;
			} else {
				bowid = 70;
			}
		}
		if (!log.equals("Normal")) {
			if (bow.equals("Shortbow")) {
				x = 115;
				y = 400;
			} else {
				x = 261;
				y = 405;
			}
		}
		log("Fletching " + log + " " + bow + "s");
		return true;
	}

	public int loop() {
		switch (state) {
			case 0: //deposit all items, withdraw logs
				RSObject Bank = findObject(bankbooth);
				if (distanceTo(Bank.getLocation()) > 3) {
					walkTileMM(Bank.getLocation());
					return random(1000, 1500);
				}
				if (!RSInterface.getInterface(762).isValid() && Bank != null) {
					atObject(Bank, "Use-quickly");
					wait(random(500, 1000));
					bank.depositAllExcept(knife);
					clickMouse(random(42, 62), random(100, 115), false);
					wait(random(500, 1000));
					atMenu("Withdraw-All");
					bank.close();
					state++;
					return random(1000, 2000);
				}
			case 1: //let's fletch
				if (!inventoryContains(logid) || inventoryContains(bowid)) { 
					state--;
					return random(500, 1000);
				}
				atInventoryItem(knife, "Use");
				wait(random(300, 1000));
				atInventoryItem(logid, "Use");
				wait(random(1000, 1500));
				if (RSInterface.getInterface(inter).isValid()) {
					clickMouse(x + random(-7, 7), y + random(-7, 7), false);
					atMenu("Make X");
					wait(random(1000, 1500));
					int rnd = random(27, 100);
					sendText("" + rnd, true);
					while (getMyPlayer().getAnimation() == -1) {
						wait(random(100, 500));
					}
					while (getMyPlayer().getAnimation() != -1) {
						wait(random(100, 200));
					}
					state--;
					return random(100, 1000);
				}
		}
		log("Error!");
		return -1;
	}
		
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<center>";
		html += "<h2>" + getName() + "</h2>";
		html += "<br />";
		html += "<b>Author:</b> " + getAuthor();
		html += "<br />";
		html += "<b>Version:</b> " + getVersion();
		html += "<br /><br />";
		html += "<b>Instructions:</b>";
		html += "<br />";
		html += "Start in Varrock west bank with <i>ONLY</i> a knife in inventory.";
		html += "<br />";
		html += "<br />";
		html += "<b>Arguments:</b>";
		html += "<br />";
		html += "<br />";
		html += "<form>";
		html += "<select name=\"logs\">";
		html += "<option value=\"Normal\">Normal</option>";
		html += "<option value=\"Oak\">Oak</option>";
		html += "<option value=\"Willow\">Willow</option>";
		html += "<option value=\"Maple\">Maple</option>";
		html += "<option value=\"Yew\">Yew</option>";
		html += "<option value=\"Magic\">Magic</option>";
		html += "</select>";
		html += "<select name=\"type\">";
		html += "<option value=\"Shaft\">Arrow Shafts</option>";
		html += "<option value=\"Short\">Shortbow</option>";
		html += "<option value=\"Long\">Longbow</option>";
		html += "</select>";
		html += "</form>";
		html += "</center>";
		html += "</html>";
		return html;
	}
}